package com.itedge.infrastructure.service.domain.impl;

import javax.persistence.NoResultException;

import org.springframework.dao.EmptyResultDataAccessException;

import com.itedge.infrastructure.domain.IProcessEntity;
import com.itedge.infrastructure.service.domain.IProcessEntityService;

/**
 * Abstract class for all JPA based services on process linked Entities.
 * 
 * @author jhe
 *
 * @param <E extends IProcessEntity>
 */
public abstract class AbstractJpaProcessEntityService<E extends IProcessEntity> 
	extends AbstractJpaEntityService<E> implements IProcessEntityService<E> {

	@Override
	public E findEntityByProcessId(String procId) {
		E result;
		try {
			result = this.getEntityManager().createQuery("select o from "
				+ this.getClassReference().getSimpleName() +
					" o where o.processInstanceId = :procId", 
						this.getClassReference()).setParameter("procId", procId)
							.getSingleResult();
		} catch(EmptyResultDataAccessException e){
        	return null;
        } catch(NoResultException e) {
	        return null;
	    }
        return result; 
	}

}
